/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.context.abstract.FileSystem;
import fever.app.context.abstract.IFileSystem;
import fever.app.context.haxe.HaxeContext;
import fever.app.context.haxe.HaxeMethod;

import swhx.Api;

/**
 * FileSystem using Haxe backend.
 *
 * @author Romain Ecarnot
 */
class fever.app.context.haxe.HaxeFileSystem extends FileSystem implements IFileSystem
{
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Constant. Haxe method name for isDirectory filesystem method. */
	public static var isDirectoryMETHOD : HaxeMethod = new HaxeMethod( 'isDirectory' );
	
	/** Constant. Haxe method name for createDirectory filesystem method. */
	public static var createDirectoryMETHOD : HaxeMethod = new HaxeMethod( 'createDirectory' );
	
	/** Constant. Haxe method name for deleteDirectory filesystem method. */
	public static var deleteDirectoryMETHOD : HaxeMethod = new HaxeMethod( 'deleteDirectory' );
	
	/** Constant. Haxe method name for renameDirectory filesystem method. */
	public static var renameDirectoryMETHOD : HaxeMethod = new HaxeMethod( 'renameDirectory' );
	
	/** Constant. Haxe method name for renameDirectory filesystem method. */
	public static var isFileMETHOD : HaxeMethod = new HaxeMethod( 'isFile' );
	
	/** Constant. Haxe method name for renameDirectory filesystem method. */
	public static var renameFileMETHOD : HaxeMethod = new HaxeMethod( 'renameFile' );
	
	/** Constant. Haxe method name for deleteFile filesystem method. */
	public static var deleteFileMETHOD : HaxeMethod = new HaxeMethod( 'deleteFile' );
	
	/** Constant. Haxe method name for readFile filesystem method. */
	public static var readFileMETHOD : HaxeMethod = new HaxeMethod( 'readFile' );
	
	/** Constant. Haxe method name for writeToFile filesystem method. */
	public static var writeToFileMETHOD : HaxeMethod = new HaxeMethod( 'writeToFile' );
	
	/** Constant. Haxe method name for appendToFile filesystem method. */
	public static var appendToFileMETHOD : HaxeMethod = new HaxeMethod( 'appendToFile' );
	
	/** Constant. Haxe method name for getTempDirectory method. */
	public static var getTempDirectoryMETHOD : HaxeMethod = new HaxeMethod( 'getTempDirectory' );
	
	/** Constant. Haxe method name for getAppDirectory method. */
	public static var getAppDirectoryMETHOD : HaxeMethod = new HaxeMethod( 'getAppDirectory' );
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function HaxeFileSystem()
	{
		super();
	}
	
	/**
	 * Returns {@code true} if passed-in {@code path} is a valid directory 
	 * on filesystem.
	 * 
	 * @param path A fully qualified path
	 */
	public function isDirectory( path : String ) : Boolean
	{
		if( !path ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( isDirectoryMETHOD ), 
			path
		);
	}
	
	/**
	 * Creates passed-in {@code path} directory on filesystem.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified path
	 */
	public function createDirectory( path : String ) : Boolean
	{
		if( !path ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( createDirectoryMETHOD ), 
			path
		);
	}
	
	/**
	 * Removes passed-in {@code path} directory from filesystem.
	 * 
	 * <p>All files insides directory are also deleted.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified path
	 */
	public function deleteDirectory( path : String ) : Boolean
	{
		if( !path ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( deleteDirectoryMETHOD ), 
			path
		);
	}
	
	/**
	 * Renames passed-in {@code path} directory with new passed-in 
	 * {@code newName} name.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified path
	 * @param newName A fully qualified path
	 */
	public function renameDirectory( path : String, newName : String ) : Boolean
	{
		if( !path || !newName ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( renameDirectoryMETHOD ), 
			path,
			newName
		);
	}
	
	/**
	 * Returns {@code true} if passed-in {@code path} is a valid file 
	 * on filesystem.
	 * 
	 * @param path A fully qualified file path
	 */
	public function isFile( path : String ) : Boolean
	{
		if( !path ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( isFileMETHOD ), 
			path
		);
	}

	/**
	 * Renames passed-in {@code path} file with new passed-in 
	 * {@code newName} name.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified file path
	 * @param newName A fully qualified file path
	 */
	public function renameFile( path : String, newName : String ) : Boolean
	{
		if( !path || !newName ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( renameFileMETHOD ), 
			path,
			newName
		);
	}
	
	/**
	 * Removes passed-in {@code path} file from filesystem.
	 * 
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified file path
	 */
	public function deleteFile( path : String ) : Boolean
	{
		if( !path ) return false;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( deleteFileMETHOD ), 
			path
		);
	}
	
	/** 
	 * Reads file text content.
	 * 
	 * @param fullQualifiedFilename A full qualified filename
	 * 
	 * @return File content or {@code null} if application can't read file.
	 */
	public function readFile( fullQualifiedFilename : String ) : String
	{
		if( !fullQualifiedFilename ) return null;
		
		return Api.call( 
			HaxeContext.getHaxeMethod( readFileMETHOD ), 
			fullQualifiedFilename
		);
	}
	
	/**
	 * Writes text data into file.
	 * 
	 * @param path ( optional ) A fully qualified path to the file you want to write.<br />
	 * Default use application starting directory.
	 * @param data String to write in file
	 */
	public function writeToFile( data : String, path : String ) : Boolean
	{
		if( !path ) return false;
			
		return Api.call( 
			HaxeContext.getHaxeMethod( writeToFileMETHOD ), 
			data,
			path
		);
	}
	
	/**
	 * Appends text data into file.
	 * 
	 * @param path ( optional ) A fully qualified path to the file you want to write.<br />
	 * Default use application starting directory.
	 * @param data String to write in file
	 */
	public function appendToFile( data : String, path : String ) : Boolean
	{
		if( !path ) return false;
			
		return Api.call( 
			HaxeContext.getHaxeMethod( appendToFileMETHOD ), 
			data,
			path
		);
	}
	
	/**
	 * Returns user "temporary" directory  from current system.
	 */
	public function getTempDirectory() : String 
	{
		return Api.call( HaxeContext.getHaxeMethod( getTempDirectoryMETHOD ) ); 
	}

	/**
	 * Returns current application path from current system.
	 */
	public function getAppDirectory() : String 
	{
		return Api.call( HaxeContext.getHaxeMethod( getAppDirectoryMETHOD ) ); 
	}
}